package com.hcb.tonguetiphealth.widget.homepage;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.hcb.tonguetiphealth.adapter.RecommendHealthResListAdapter;
import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.manager.JSONManager;
import com.hcb.tonguetiphealth.model.Restaurant;
import com.hcb.tonguetiphealth.util.HttpUtils;
import com.hcb.tonguetiphealth.util.MessageWhat;
import com.hcb.tonguetiphealth.util.StringUtils;
import com.hcb.tonguetiphealth.view.R;
import com.hcb.tonguetiphealth.widget.GeneralWidget;

public class HomepageWatchedResWidget extends GeneralWidget implements OnClickListener {
	private static final String TAG = "HomepageWatchedResWidget";
	private String url_res = Global.URL + Global.servlet_watchedRes;
	
	private Activity ctx;
	private Handler mHandler;
	private LinearLayout ll_back;
	private ListView listView;
	private RecommendHealthResListAdapter resAdapter;
	private List<Restaurant> restaurantList; //商家
	
	public HomepageWatchedResWidget(Context context) {
		super(context);
		ctx = (Activity)context;
		inflate(context, R.layout.homepage_personal_watched_res, this); 
		
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch(msg.what) {
				case MessageWhat.SHOW_DIALOG:
					showMessageDialog(ctx, "正在获取商户列表...");
					break;
				case MessageWhat.DIMISS_DIALOG:
					listView.setAdapter(resAdapter);
					dismissMessageDialog();
					break;
				}
			}
		};
		ll_back = (LinearLayout)findViewById(R.id.homepage_watched_res_back_button);
		ll_back.setOnClickListener(this);
		
		listView = (ListView)findViewById(R.id.homepage_watched_res_listview);
		
		initData();
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				Restaurant restaurant = (Restaurant) arg0.getAdapter().getItem(arg2);
			}
		});
		
	}
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.homepage_watched_res_back_button:
			ctx.setContentView(new HomepagePersonalWidget(ctx));
			break;
		default:
			break;
		}
	}
	
	private void initData() {
		restaurantList = new ArrayList<Restaurant>();
		resAdapter = new RecommendHealthResListAdapter(ctx, restaurantList);
		
		downRes();
	}
	
	private void downRes() {
		mHandler.sendEmptyMessage(MessageWhat.SHOW_DIALOG);
		new Thread() {
			public void run() {
				restaurantList.clear();
				String response = null;
				try {
					List <NameValuePair> params = new ArrayList <NameValuePair>();
					params.add(new BasicNameValuePair("cityId", "129"));
					params.add(new BasicNameValuePair("lng", "117.28"));
					params.add(new BasicNameValuePair("lat", "31.86"));
					response = HttpUtils.Post(url_res, params);
					
					if(Global.DEBUG) {
						System.out.println("downRes -----------------------------\n" + response);
					}
				} catch(Exception e) {
					Log.e(TAG, "Exception", e);
				}
				if(!StringUtils.isEmpty(response)) {
					JSONManager jManager = new JSONManager();
					Restaurant restaurant;
					boolean isGotJSONObject = jManager.isGotJSONObject(response);
					if(isGotJSONObject) {
						JSONArray jList = jManager.getJsonList();
						if(null == jList) {
							
						} else {
							for(int i=0; i< jList.length(); i++) {
								JSONObject jObject = getJSONObject(jList, i);
								if(null != jObject) {
									restaurant = new Restaurant();
									restaurant.setId(getString(jObject, "userid"));
									restaurant.setName(getString(jObject, "company"));
									restaurant.setLevel(StringUtils.parseFloat(getString(jObject, "level")));
									restaurant.setType(getString(jObject, "type"));
									restaurant.setDistance(getString(jObject, "distance"));
									restaurant.setImgURL(getString(jObject, "thumb"));//imgURL

									restaurantList.add(restaurant);
								}
							}
						}
						int count = jManager.getResult();
						String msg = jManager.getMsg();
						System.out.println("------ 得到msg: " + count + ":" + msg);
					}
				}
				mHandler.sendEmptyMessage(MessageWhat.DIMISS_DIALOG);
			}
		}.start();
	}
	
	@Override
	public void notifyAdapter() {
		
	}
}
